<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ItemController
 *
 * @author Samsung
 */
class ItemController extends Controller {
    
    private $_model;
    private $_board;
    
    public function actions() {
        return array(
            'defaultimage'=>array(
                'class'=>'ext.actions.GalleryAction',
                'module' => 'Item',
                'moduleId'=>Yii::app()->request->getParam('id'),
                'imageId'=>Yii::app()->request->getParam('imageId'),
                'action'=>'default',
            ),
            'deleteimage'=>array(
                'class'=>'ext.actions.GalleryAction',
                'module' => 'Item',
                'moduleId'=>Yii::app()->request->getParam('id'),
                'imageId'=>Yii::app()->request->getParam('imageId'),
                'action'=>'delete',
            ),
            'defaultvideo'=>array(
                'class'=>'ext.actions.VideoAction',
                'module' => 'Item',
                'moduleId'=>Yii::app()->request->getParam('id'),
                'videoId'=>Yii::app()->request->getParam('videoId'),
                'action'=>'default',
            ),
            'deletevideo'=>array(
                'class'=>'ext.actions.VideoAction',
                'module' => 'Item',
                'moduleId'=>Yii::app()->request->getParam('id'),
                'videoId'=>Yii::app()->request->getParam('videoId'),
                'action'=>'delete',
            ),
            'shortlistItem'=>array(
                'class'=>'ext.widgets.EShortlist.ShortListAction',
                'module'=> 'Item',
                'moduleId'=>Yii::app()->request->getParam('id'),
            ),
            'likeItem'=>array(
                'class'=>'ext.widgets.ELike.LikeAction',
                'module'=> 'Item',
                'moduleId'=>Yii::app()->request->getParam('id'),
            ),
            'abuseItem'=>array(
                'class'=>'ext.widgets.EAbuse.AbuseAction',
                'module'=> 'Item',
                'moduleId'=>Yii::app()->request->getParam('id'),
            ),
        );
    }
    
    public function init() {
        parent::init();
        
        $this->breadcrumbs = array(
            Lang::t('merchant.page.breadcrumbs') => Yii::app()->createUrl('merchant/site/index'),
        );
    }
    
    /**
     * Item 
     */
    /**
     * Displays a particular model.
     * @param integer $id the ID of the model to be displayed
     */
    public function actionView($id) {
        $this->render('view', array(
            'data' => $this->loadModel($id),
        ));
    }

    /**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionCreate() {
        
        $model = new Item('userCreate');
        $model->autoFill();

        // Uncomment the following line if AJAX validation is needed
        $this->performAjaxValidation($model);
        $form = Yii::app()->request->getParam(get_class($model));
        if ($form) {
            $model->attributes = $form;
            $model->image_= CUploadedFile::getInstances($model, 'image_');
            $model->video_=ActiveForm::getVideoInstance($model, 'video_');
            if ($model->addRecord()) {
                $this->setFlash('success', Lang::t('merchant.item.create.flash.success'));
                //$this->redirect(array('update', 'id' => $model->id, 'boardId'=>$model->boardId));
            }else{
                $this->setFlash('error', Lang::t('merchant.item.create.flash.fail'));
                if($this->debugMode) print_r($model->getErrors());
            }
        }
        
        $this->render('createForm', array(
            'model' => $model,
            'formConfig' => array(
                'id' => $this->formId,
                'enableAjaxValidation' => true,
                'enableClientValidation' => true,
                'clientOptions' => array(
                    'validateOnSubmit' => true,
                ),
                'htmlOptions' => array(
                    'enctype' => 'multipart/form-data',
                ),
                'focus' => array($model, 'title_'),
            ),
        ));
    }

    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id the ID of the model to be updated
     */
    public function actionUpdate($id) {
        
        $model = $this->loadModel($id);
        $model->scenario = 'userUpdate';
        $model->autoFill();
        
        $this->performAjaxValidation($model);
        $form = Yii::app()->request->getParam(get_class($model));
        if (!empty($form)) {
            $model->attributes = $form;
            $model->image_= CUploadedFile::getInstances($model, 'image_');
            $model->imageEditor_= ActiveForm::getFormInstance($form, 'imageEditor_');
            $model->video_=ActiveForm::getVideoInstance($model, 'video_');
            $model->videoEditor_= ActiveForm::getFormInstance($form, 'videoEditor_');            
            if ($model->updateRecord()) {
                $model->refresh();
                $this->setFlash('success', Lang::t('merchant.item.update.flash.success'));
                $this->refresh();
            }else{
                $this->setFlash('error', Lang::t('merchant.item.update.flash.fail'));
                if($this->debugMode) print_r($model->getErrors());
            }
        }

        $this->render('updateForm', array(
            'model' => $model,
            'board'=>$this->_board,
            'formConfig' => array(
                'id' => $this->formId,
                'enableAjaxValidation' => true,
                'enableClientValidation' => true,
                'clientOptions' => array(
                    'validateOnSubmit' => true,
                ),
                'htmlOptions' => array(
                    'enctype' => 'multipart/form-data',
                ),
                'focus' => array($model, 'firstName_'),
            ),
        ));
    }

    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'admin' page.
     * @param integer $id the ID of the model to be deleted
     */
    public function actionDelete($id) {
        $model=$this->loadModel($id);
        $model->scenario='userDelete';
        if($model->deleteRecord()){
            $this->setFlash('success', Lang::t('merchant.item.delete.flash.success'));
        }else{
            $this->setFlash('error', Lang::t('merchant.item.delete.flash.fail'));
            if($this->debugMode) print_r($model->errors);
        }

        // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
        if (!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
    }

    /**
     * Lists all models.
     */
    public function actionIndex() {
        $dataProvider = new CActiveDataProvider('Item');
        $this->render('index', array(
            'dataProvider' => $dataProvider,
        ));
    }

    /**
     * Manages all models.
     */
    public function actionAdmin() {
        $model = new Item('userSearch');
        $model->unsetAttributes();  // clear any default values
        $model->userId_=Yii::app()->user->id;
        
        $form = Yii::app()->request->getParam(get_class($model));
        if (!empty($form))
            $model->attributes = $form;
        
        $this->render('admin', array(
            'model' => $model,
            'dataProvider'=>$model->userSearch(),
        ));
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    public function loadModel($id = null) {
        if ($this->_model === null) {
            if (!empty($id))
                $this->_model = Item::model()->accessible()->findByPk((int) $id);

            if ($this->_model === null)
                HttpStatus::code(404);
        }
        return $this->_model;
    }
    
    public function menu(){
        return array(
            array('label'=>Lang::t('merchant.menu.add.image'), 'visible'=>in_array($type, array(Option::ITEM_TYPE_IMAGE)), 'url'=>array('create')),
            array('label'=>Lang::t('merchant.menu.add.video'), 'visible'=>in_array($type, array(Option::ITEM_TYPE_VIDEO)), 'url'=>array('create')),
            array('label'=>Lang::t('merchant.menu.add.article'), 'visible'=>in_array($type, array(Option::ITEM_TYPE_ARTICLE)), 'url'=>array('create')),
            //array('label'=>Lang::t('merchant.menu.add.event'), 'visible'=>in_array($type, array(Option::ITEM_TYPE_EVENT)), 'url'=>array('create')),
            //array('label'=>Lang::t('merchant.menu.add.product'), 'visible'=>in_array($type, array(Option::ITEM_TYPE_PRODUCT)), 'url'=>array('create')),
        );
    }
    
    public function gridThumbnail($data, $row){
        switch($data->typeCode){
            case Option::ITEM_TYPE_IMAGE:
                echo CHtml::image($data->thumbnail);
                break;
            case Option::ITEM_TYPE_VIDEO:
                echo $data->embedVideo;
                break;
        }
    }
    
    public function renderForm(&$model, &$formConfig){
        switch($model->typeCode){
            case Option::ITEM_TYPE_IMAGE:
                return $this->renderPartial('_imageForm', array('model'=>$model, 'formConfig'=>$formConfig));
                break; 
            case Option::ITEM_TYPE_VIDEO:
                return $this->renderPartial('_videoForm', array('model'=>$model, 'formConfig'=>$formConfig));
                break; 
            case Option::ITEM_TYPE_ARTICLE:
                return $this->renderPartial('_articleForm', array('model'=>$model, 'formConfig'=>$formConfig));
                break; 
            case Option::ITEM_TYPE_EVENT:
                return $this->renderPartial('_eventForm', array('model'=>$model, 'formConfig'=>$formConfig));
                break; 
        };
    }
}